package cn.gxljc.technology.store_management.service.superadminservice.components;

import cn.gxljc.technology.store_management.entity.Warehouse;
import cn.gxljc.technology.store_management.mapper.WarehouseMapper;
import cn.gxljc.technology.store_management.service.superadminservice.modules.RepositoryManagement;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 该类主要实现仓库业务接口
 */
@Component
public class RepositoryManagementImpl implements RepositoryManagement {

    @Resource
    private WarehouseMapper warehouseMapper;

    @Override
    public Integer createRepository(int repositoryID, String repositoryName, String repositoryLocation, String adminName) {
        //非空判断
        if (repositoryName == null || repositoryLocation == null || adminName == null) {
            System.out.println("RepositoryManagementImpl/createRepository/参数为空");
            return -1;
        }

        //查找数据是否存在
        Warehouse warehouse1 = warehouseMapper.selectWarehouseByID(repositoryID);
        if (warehouse1 != null) {
            System.out.println("RepositoryManagementImpl/createRepository/仓库重复");
            return -2;
        }

        //插入数据
        Warehouse warehouse = new Warehouse();
        warehouse.setWarehouseID(repositoryID);
        warehouse.setWarehouseName(repositoryName);
        warehouse.setWarehouseAddress(repositoryLocation);
        warehouseMapper.insertWarehouse(warehouse);

        return 0;
    }

    @Override
    public Integer deleteRepository(int repositoryID) {
        Warehouse warehouse = warehouseMapper.selectWarehouseByID(repositoryID);
        if (warehouse == null) {
            System.out.println("RepositoryManagementImpl/deleteRepository/不存在该仓库");
            return -2;
        }
        boolean b = warehouseMapper.deleteWarehouseByID(repositoryID);
        if (!b) return -1;
        return 0;
    }

    @Override
    public List<Warehouse> findAllRepository() {
        List<Warehouse> warehouses = warehouseMapper.selectAllWarehouse();
        if (warehouses == null) {
            System.out.println("RepositoryManagementImpl/findAllRepository/没有查找任何数据");
            return new ArrayList<>();
        }
        if (warehouses.size() == 0) {
            System.out.println("RepositoryManagementImpl/findAllRepository/没有查找任何数据");
            return new ArrayList<>();
        }
        return warehouses;
    }

    @Override
    public Integer isRepositoryExist(int repositoryID) {
        Warehouse warehouse = warehouseMapper.selectWarehouseByID(repositoryID);
        if (warehouse == null) return 0;
        return 1;
    }

    @Override
    public Integer changeRepositoryInfo(int repositoryID, String repositoryName, String repositoryLocation) {
        if (repositoryName == null || repositoryLocation == null) {
            System.out.println("RepositoryManagementImpl/changeRepositoryInfo/参数为空");
            return -1;
        }

        Integer repositoryExist = this.isRepositoryExist(repositoryID);
        if (repositoryExist == -1) {
            System.out.println("RepositoryManagementImpl/changeRepositoryInfo/参考不存在");
            return -2;
        }

        Warehouse warehouse = new Warehouse();
        warehouse.setWarehouseID(repositoryID);
        warehouse.setWarehouseName(repositoryName);
        warehouse.setWarehouseAddress(repositoryLocation);
        warehouseMapper.updateWarehouseByID(warehouse);

        return 0;
    }

    @Override
    public Warehouse findRepository(int repositoryID) {
        Integer repositoryExist = this.isRepositoryExist(repositoryID);
        if (repositoryExist == -1) {
            System.out.println("RepositoryManagementImpl/findRepository/参数为空");
            return null;
        }
        return warehouseMapper.selectWarehouseByID(repositoryID);
    }
}
